import { any } from "../core/logic"
import {Texture} from './Texture';
import { Engine } from "../Engine";
import { WebTextureFormat } from "../core/color";
class TextureCompressedArrayBuffer extends Texture{
    constructor(opts){
        super(opts);
        this.usage = any(opts.usage, GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST)
    }
    get dimension() {
        return this._dimension;
    }
    set dimension(v) {
        this._dimension = '2d';
    }
    get viewDimension() {
        return this._viewDimension;
    }
    set viewDimension(v) {
        this._viewDimension = '2d';
    }
    get image(){
        return this._image
    }
    set image(v) {
        this._image = v;
        this.format = v.internalFormat;
    }
    toGPU() {
        if(!this.image) {
            return
        }
        let data = this.image;
         //压缩纹理
         const wtFormat = WebTextureFormat[this.format];
         const blockInfo = wtFormat.compressed || { blockBytes: 4, blockWidth: 1, blockHeight: 1 };
         const texture = Engine.instance.device.createTexture({
             dimension: this.dimension,
             size: [data[0].width, data[0].height, 1],
             format: this.format,
             usage: this.usage,
             mipLevelCount: data.length,
         });
         data.map((item) => {
             let { width, height, data: bufferData } = item
             const bytesPerRow = Math.ceil(width / blockInfo.blockWidth) * blockInfo.blockBytes;
             Engine.instance.device.queue.writeTexture(
                 {
                     texture: texture,
                 },
                 bufferData,
                 {
                     offset: 0,
                     bytesPerRow: bytesPerRow
                 },
                 {
                     width: width,
                     height: height,
                 }
             );
         })
        this.image=null
        let { dimension, viewDimension, format, usage, sampler, multisampled, sampleType, uvIndex, uvTransform } = this;
        return {
            dimension,
            viewDimension,
            format,
            usage,
            sampler,
            multisampled,
            sampleType,
            uvIndex,
            uvTransform,
            texture: texture
        }
    }
}
export {TextureCompressedArrayBuffer}